﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.UI.UserControls;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Security.Permissions;

namespace company_present
{
    public partial class EditNews : ModuleCompanyBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //kiem tra co quyen edit de xoa khong
            ModulePermissionController per = new ModulePermissionController();

            if (!IsPostBack)
            {

                //1. kiem tra neu ton tai du lieu nen form sua
                if (Request["newsid"] != null)
                {

                    int newsid;
                    //kiem tra la id hop le
                    if (int.TryParse(Request["newsid"], out newsid))
                    {
                        var news = (from n in DB.cp_News
                                    where n.ID == newsid
                                    select n).FirstOrDefault();

                        //kiem tra ton tai tin 
                        if (news != null)
                        {
                            // load du tieu de
                            this.Title.Text = news.Title;
                            //hien thi anh dai dien tin
                            this.img.ImageUrl = PathUrlImage + news.Img;
                            //hien trang thai an hie
                            this.isVisible.Checked = news.Visible.Value;
                            //hien thi mo ta
                            (this.txtDes as TextEditor).Text =  news.Description;
                            //hien thi text da nhap
                            (this.txtContent as TextEditor).Text = news.Content;
                           

                        }
                    }
                }

            }
        }


        //luu noi dung cap nhat
        protected void Button1_Click(object sender, EventArgs e)
        {
            //update tin tuc


            if ((Request["newsid"] != null) && (Title.Text != null) && ((DotNetNuke.UI.UserControls.TextEditor)txtDes).Text != null && ((DotNetNuke.UI.UserControls.TextEditor)txtContent).Text != null)
            {

                int newsid;
                //kiem tra so nguye
                if (int.TryParse(Request["newsid"], out newsid))
                {

                    //cap nhat noi dung news
                    var news = (from n in DB.cp_News where n.ID == newsid select n).FirstOrDefault();
                    if (news != null)
                    {

                        //luu lai anh neu chon anh mo ta moi
                        if (this.ImageUpload.HasFile)
                        {
                            string filename = Guid.NewGuid() + ImageUpload.FileName;
                            ImageUpload.SaveAs(PathPhysicalImage + filename);
                            news.Img = filename;
                        }

                        //cap nhat noi dung , tieu de , mo ta 
                        news.Title = Title.Text;
                        news.Visible = isVisible.Checked;
                        news.Description =(txtDes as TextEditor).Text;
                        news.Content = (txtContent as TextEditor).Text;
                        DB.SubmitChanges();
                    }


                }

            }
        }
    }
}